package com.decent.common.aspect;

import cn.hutool.core.util.StrUtil;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.common.exception.ErrorCodeException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 接口请求参数中查询/下载时间的限时7天的检查
 *
 * @author: wangyan
 * @date: 2021/12/16
 */
@Aspect
@Configuration
@Slf4j
public class CheckDataTimeAdvice {
    /**
     * 查询/下载限制时间天数
     */
    private static final Integer INTERNAL_DAYS = 7;
    @Resource
    private HttpServletRequest request;

    @Pointcut("@annotation(com.decent.common.annotation.CheckDataTime)")
    public void executeService() {
    }

    /**
     * 查询/下载时间校验，不超过7天
     *
     * @param pjp 连接点
     * @throws Throwable 错误
     */
    @Before("executeService()")
    public void before(JoinPoint pjp) {
        Object[] methodArgs = pjp.getArgs();
        if (methodArgs == null) {
            throw new ErrorCodeException(ErrorCodeEnum.INVALID_PARAMS, "参数为空");
        }
        // 标记有检验通过
        boolean checkPass = false;
        // 参数名为queryStartTime/queryEndTime的情况
        String queryStartTime = request.getParameter("queryStartTime");
        String queryEndTime = request.getParameter("queryEndTime");
        if (StrUtil.isNotBlank(queryStartTime)) {
            if (!inspectionInterval(queryStartTime, queryEndTime)) {
                throw new ErrorCodeException(ErrorCodeEnum.THE_INTERVAL_OF_DATA_IS_TOO_LONG);
            }
            checkPass = true;
        }
        // 参数名为startTime/endTime的情况
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        if (StrUtil.isNotBlank(startTime)) {
            if (!inspectionInterval(startTime, endTime)) {
                throw new ErrorCodeException(ErrorCodeEnum.THE_INTERVAL_OF_DATA_IS_TOO_LONG);
            }
            checkPass = true;
        }
        // 检验不通过，返异常
        if (!checkPass) {
            throw new ErrorCodeException(ErrorCodeEnum.THE_INTERVAL_OF_DATA_IS_TOO_LONG);
        }
    }

    /**
     * 检查时间间隔是否在限定时间天数内
     *
     * @param queryStartTime 查询开始时间
     * @param queryEndTime   查询结束时间
     * @return 是/否
     */
    private boolean inspectionInterval(String queryStartTime, String queryEndTime) {
        LocalDate queryStartDate = queryStartTime.indexOf(' ') > -1 ?
                LocalDate.parse(queryStartTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
                : LocalDate.parse(queryStartTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate queryEndDate = StrUtil.isBlank(queryEndTime) ?
                LocalDate.now() : (queryEndTime.indexOf(' ') > -1 ?
                LocalDate.parse(queryEndTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
                : LocalDate.parse(queryEndTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        // 时间间隔判断，限制只能查询/下载7天数据
        if (queryStartDate.plusDays(INTERNAL_DAYS).compareTo(queryEndDate) <= 0) {
            return false;
        }
        return true;
    }
}
